//引入angular核心组件
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
//引入类
import { RoomType } from '../../domain/roomType';
//引入服务
import { RoomTypeService } from '../../service/room-type.service';

declare var layui: any;

@Component({
  selector: 'app-room-type-list',
  templateUrl: './room-type-list.component.html',
  styleUrls: ['./room-type-list.component.css'],
  providers: [RoomTypeService]
})
export class RoomTypeListComponent implements OnInit {

  roomTypes: RoomType[];
  selectedRoomTypeId: number;

  roomType = new RoomType();

  haveTv: string;
  haveRef: string;
  haveAirc: string;

  shiFous = ['yes', 'no'];

  constructor(
    private roomTypeService: RoomTypeService,
    private router: Router
  ) { }

  ngOnInit() {

    layui.use(['element', 'layer', 'form', 'table'], function () {
      var element = layui.element;
      var layer = layui.layer;
      var form = layui.form;
      var table = layui.table;
      element.render();
      form.render();

    });

    //组件加载初期查询所有房间类型
    this.roomTypeService.getRoomTypes()
      .then(res => this.roomTypes = res);
  }

  //选择一个房间类型
  isSelect(roomType: RoomType) {
    return roomType.id === this.selectedRoomTypeId;
  }

  //路由到选择房间类型的详细页面
  onSelect(roomType: RoomType) {
    this.router.navigate(['/room-type-detail', roomType.id]);
  }

  //新建一个RoomType
  createRoomType(): void {
    //this.roomTypeCreate.haveTV = 
    //this.roomTypeCreate.haveRef = document.getElementById('haveRef').textContent;
    //this.roomTypeCreate.haveAirc = document.getElementById('haveAirc').textContent;

    this.roomTypeService.createRoomType(this.roomType)
      .then(res => {
        console.log(res);
      })
  }

  //删除一个RoomType
  deleteRoomType(roomType: RoomType) {
    this.roomTypeService.deleteRoomType(roomType);
  }

  //重置
  reset(): void {
    location.reload();
  }

}
